﻿Class GridComboBox : Inherits ComboBox
	Implements IDataGridViewEditingControl

	Private dataGridViewControl As DataGridView
	Private valueIsChanged As Boolean = False
	Private rowIndexNum As Integer

	Sub New()
		MyBase.New()
		UseEnterAsTab = False
		Results.HandleEnter = True
	End Sub

	Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
		Get
			Return Me.valueIsChanged.ToString
		End Get
		Set(ByVal value As Object)
			If TypeOf value Is [String] Then
				Me.Text = value.ToString
			End If
		End Set
	End Property

	Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
		Return Me.Value
	End Function

	Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
		Me.Font = dataGridViewCellStyle.Font
		Me.ForeColor = dataGridViewCellStyle.ForeColor
		Me.BackColor = dataGridViewCellStyle.BackColor
	End Sub

	Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
		Get
			Return rowIndexNum
		End Get
		Set(ByVal value As Integer)
			rowIndexNum = value
		End Set
	End Property

	Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
		e.Graphics.Clear(Color.White)
		Dim R = ClientRectangle
		If Focused Or DroppedDown Then e.Graphics.FillRectangle(SystemColors.Highlight.Opacity(128).Brush, R)
	End Sub

	Public Overrides Sub SelectItem()
		MyBase.SelectItem()
		SendKeys.Send("{ENTER}")
	End Sub

	Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
		Return True
	End Function

	Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
		'DroppedDown = True
	End Sub

	Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
		Get
			Return False
		End Get
	End Property

	Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
		Get
			Return dataGridViewControl
		End Get
		Set(ByVal value As DataGridView)
			dataGridViewControl = value
		End Set
	End Property

	Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
		Get
			Return valueIsChanged
		End Get
		Set(ByVal value As Boolean)
			valueIsChanged = value
		End Set
	End Property

	Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
		Get
			Return MyBase.Cursor
		End Get
	End Property

	Protected Overrides Sub OnTextChanged(ByVal eventargs As EventArgs)
		valueIsChanged = True
		If Not EditingControlDataGridView Is Nothing Then Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
		MyBase.OnTextChanged(eventargs)
	End Sub

	'Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
	'	Dim R = ClientRectangle
	'	e.Graphics.FillRectangle(BackColor.Brush, R)
	'	e.Graphics.FillRectangle(SystemColors.Highlight.Opacity(128).Brush, R)
	'	'R.Inflate(2, 2)
	'	R = ClientRectangle
	'	R.Offset(Padding.Left, Padding.Top)
	'	R.Width -= Padding.Horizontal : R.Height -= Padding.Vertical
	'	Dim Flags As New TextFormatFlags
	'	'Flags = TextFormatFlags.NoPadding
	'	TextRenderer.DrawText(e.Graphics, Text, Font, R.Location, ForeColor, Color.Transparent, Flags)
	'	'DrawText2(e.Graphics, Text, Font, ForeColor, R, StrFormat("lm"))
	'End Sub

End Class

